package com.leostadyn.stackfarm.timestatistics.view.activity.admin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.StaffTimeRecordBean;
import com.leostadyn.stackfarm.timestatistics.presenter.admin.FetchListPresenter;
import com.leostadyn.stackfarm.timestatistics.utils.DateUtils;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.leostadyn.stackfarm.timestatistics.view.activity.MVPActivity;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.project.ProjectListActivity;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff.StaffListActivity;
import com.leostadyn.stackfarm.timestatistics.view.activity.setting.SettingActivity;
import com.leostadyn.stackfarm.timestatistics.view.fragment.PersonnelFragment;
import com.leostadyn.stackfarm.timestatistics.view.fragment.ProjectFragment;
import com.leostadyn.stackfarm.timestatistics.view.vinterface.admin.IFetchListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ManagementActivity extends MVPActivity<IFetchListView, FetchListPresenter> implements IFetchListView, View.OnClickListener, ProjectFragment.ProjectCallBack, PersonnelFragment.PersonnelCallBack, PersonnelFragment.SetPersonnelCallBack, ProjectFragment.SetProjectCallBack {

    ImageView settingsImg;
    Button personnelBtn;
    Button projectBtn;
    ViewPager viewPager;
    TabLayout tabLayout;
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");

    String start = formatter.format(DateUtils.getDayOfWeek(Calendar.MONDAY, -1));
    String end = formatter.format(DateUtils.getDayOfWeek(Calendar.SUNDAY, -1));

    boolean isProject = true;

    List<Fragment> fragments = new ArrayList<>();
    List<String> titles = new ArrayList<>();

    PersonnelFragment personnelFragment = new PersonnelFragment();
    ProjectFragment projectFragment = new ProjectFragment();

    ArrayList<DetailProjectBean> projectBeans = new ArrayList<>();
    ArrayList<EmployeeBean> employeeBeans = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_main);
        initView();
        mPresenter.refresh(end, start, compositeDisposable);
    }

    void initView() {
        settingsImg = findViewById(R.id.img_settings);
        findViewById(R.id.nav_img_back).setVisibility(View.GONE);
        findViewById(R.id.nav_btn_add).setVisibility(View.GONE);
        TextView titleTxt = findViewById(R.id.nav_txt_title);
        titleTxt.setText(getResources().getString(R.string.management_panel));
        personnelBtn = findViewById(R.id.btn_management_personnel);
        projectBtn = findViewById(R.id.btn_management_project);
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.layout_tab);

        settingsImg.setOnClickListener(this);
        projectBtn.setOnClickListener(this);
        personnelBtn.setOnClickListener(this);

        titles.add(getResources().getString(R.string.project_hours));
        titles.add(getResources().getString(R.string.manpower_hours));

        fragments.add(projectFragment);
        fragments.add(personnelFragment);

        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });

        tabLayout.setupWithViewPager(viewPager);
    }

    void initFragment() {
        if (fragments != null) {
            fragments.clear();
        }
        projectFragment = ProjectFragment.newInstance(projectBeans);
        personnelFragment = PersonnelFragment.newInstance(employeeBeans);
        fragments.add(projectFragment);
        fragments.add(personnelFragment);

        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });
        if (isProject)
            viewPager.setCurrentItem(0);
        else
            viewPager.setCurrentItem(1);
    }

    @Override
    protected FetchListPresenter createPresenter() {
        return new FetchListPresenter();
    }

    @Override
    public void showWaiting() {

    }

    @Override
    public void dismissWaiting() {

    }

    @Override
    public void disconnected() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_settings:
                startActivity(new Intent(this, SettingActivity.class));
                break;
            case R.id.btn_management_personnel:
                startActivity(new Intent(this, StaffListActivity.class));
                break;
            case R.id.btn_management_project:
                startActivity(new Intent(this, ProjectListActivity.class));
                break;
        }

    }


    @Override
    public void sentPersonnelMessage() {
        isProject = false;
        mPresenter.refresh(end, start, compositeDisposable);
    }

    @Override
    public void setProjectList(ArrayList<EmployeeBean> arrayList) {

    }

    @Override
    public String setPersonnelTitle() {
        return "上周总工时";
    }

    @Override
    public void setProjectWorkingHours(double d) {

    }

    @Override
    public void sentProjectMessage() {
        isProject = true;
        mPresenter.refresh(end, start, compositeDisposable);
    }

    @Override
    public void setPersonnelList(ArrayList<DetailProjectBean> arrayList) {

    }

    @Override
    public String setProjectTitle() {
        return "上周总工时";
    }

    @Override
    public void setPersonnelWorkingHours(double d) {

    }

    @Override
    public void successProjectMessage(ArrayList<DetailProjectBean> projectList) {

    }

    @Override
    public void successEmployeeMessage(ArrayList<EmployeeBean> employeeList) {

    }

    @Override
    public void successMessage(ArrayList<DetailProjectBean> projectList, ArrayList<EmployeeBean> employeeList) {
        projectBeans = projectList;
        employeeBeans = employeeList;
        initFragment();
    }

    @Override
    public void failureMessage(String s) {
        ToastUtil.show(this, s);
    }

    @Override
    public void successTimeRecord(StaffTimeRecordBean staffTimeRecordBean) {

    }


    @Override
    public void mainPersonnelClick(int position, List<EmployeeBean> dataBeanList) {
        Intent intent = new Intent(this, DetailPersonnelActivity.class);
        intent.putExtra("name", dataBeanList.get(position).getName());
        intent.putExtra("uid", dataBeanList.get(position).getUid());
        intent.putExtra("job", dataBeanList.get(position).getPostName());
        startActivity(intent);
    }

    @Override
    public void mainProjectClick(int position, List<DetailProjectBean> dataBeanList) {
        Intent intent = new Intent(this, DetailProjectActivity.class);
        intent.putExtra("projectName", dataBeanList.get(position).getProjectName());
        intent.putExtra("projectCode", dataBeanList.get(position).getProjectCode());
        intent.putExtra("projectId", dataBeanList.get(position).getProjectId());
        intent.putExtra("projectWorkingHours", dataBeanList.get(position).getProjectWorkingHours());
        startActivity(intent);
    }
}
